/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ilookinto.doexe;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author zwu
 */
public class Processor {

  public static void main(String s[]) throws Exception {
    Pair<PrintWriter, Scanner, Scanner> pair = processing(new String[]{"src\\site\\WEB-INF\\resources\\2RTF"});
    PrintWriter pw = pair.first;

    //pw.write("http://www.jfic2010.org/Instructionsforauthors2010.doc\n");
    //pw.write("http://localhost:9080/ilookinto/local?node=com/cat/www/cda/files/2227148/7/M01PR10final.doc\n");
    //pw.write("c:\\zwu\\temp\\tttt.rtf\n");

    pw.write("http://localhost:9080/ilookinto/local?node=edu/dartmouth/www/%7Eacskills/docs/planning_well.doc$$$$C:\\zwu\\temp.rtf\n");
    pw.write("Q\n");
    pw.close();



  }
  static String[] para;
  static boolean getOutDaemon = false;

  public static Pair<PrintWriter, Scanner, Scanner> newStance() throws Exception {
   getOutDaemon = true;
   Thread.currentThread().sleep(10000);
   getOutDaemon = false;
    return  processing(para);
  }
  
  public static Pair<PrintWriter, Scanner, Scanner> processing(String args[]) throws Exception {
    para = args;
    ProcessBuilder pb = new ProcessBuilder(args);
    Process p = pb.start();
    final Scanner out = new Scanner(p.getInputStream());
    final Scanner error = new Scanner(p.getErrorStream());

    new Thread() {

      {
        this.setDaemon(true);
      }

      public void run() {
        try {
          while (out.hasNextLine() && !getOutDaemon ) {
            System.out.println(out.nextLine());
          }
        } catch (Exception e) {
        }
      }
    }.start();
    new Thread() {

      {
        this.setDaemon(true);
      }

      public void run() {

        while (error.hasNextLine() && !getOutDaemon ) {
          System.err.println(error.nextLine());
        }
      }
    }.start();
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(p.getOutputStream()));
    Pair pair = new Pair(pw, out, error);
    pair.exeArgs = args;
    return pair;

  }

  public static class Pair<T, S, G> {

    public String[] exeArgs;

    public Pair(T f, S s, G g) {
      first = f;
      second = s;
      third = g;
    }

    public String toString() {
      return "(" + first.toString() + ", " + second.toString() + ")";
    }
    public T first;
    public S second;
    public G third;
  }
}
//C:\myprojs\ilookinto\J\ilookinto\build\web\data\special%20folders\emails\localhost\5F948FB4FFF133CCF9B611767996D2E5\mono-dd6c1ad8-4ca7-4ac4-a5d1-b56698ed7962.pdf
//C:\Users\admin\AppData\Local\Temp\\tmp6505.rtf
//C:\myprojs\ilookinto\J\ilookinto\build\web\data\special%20folders\emails\localhost\5F948FB4FFF133CCF9B611767996D2E5\mono-dd6c1ad8-4ca7-4ac4-a5d1-b56698ed7962.pdf
